


var totalnowsize=0;
var targetbirdname="";
var targetbirdfilename="";
var folderbirdname="";
var widthbird=0;
var heightbird=0;
var idmode=0;
var arraydatamode={};

function checkBirdtypeexit(namefile){
var ext = namefile.split('.').pop().toLowerCase();
if($.inArray(ext, ['jpg', 'jpeg', 'png', 'gif']) == -1) {
    alert('invalid extension!');
    return false;
}else
{
    return true;
}
}

function handleBirdFileSelect(evt) {
    
         
    var f = evt.target.files[0]; // FileList object
         
    if(checkBirdtypeexit(f.name)===false)
     {
     
     return false;
     }  
  
      var indexset=evt.target.id.split("-");  
      datasendBirdajax(f,indexset[1]);

  
}

function resizeBirdimg(file,idindex){
   // console.log(arraydatamode[idindex]['width']);
  //  console.log(arraydatamode[idindex]['height']);
      if(arraydatamode[idindex]['nocrop'])
      {
        sendBirdfiledata(file,idindex);
      
      }else
      {
      canvasResize(file, {
                        width: arraydatamode[idindex]['width'],
                        height: arraydatamode[idindex]['height'],
                        crop: true,
                        quality: 80,
                        callback: function(data, width, height) {
                        
                                          var f = canvasResize('dataURLtoBlob', data);
                                          //console.log(arraydatamode[idindex]['width']);
                                          //console.log(width); 
                                          if((arraydatamode[idindex]['width']==width&&arraydatamode[idindex]['height']==height)||arraydatamode[idindex]['checkvalidate'])
                                          {
                                          sendBirdfiledata(f,idindex);
                                          
                                          }else
                                          {
                                          alert("รูปของคุณต้องขนาด กว้าง "+arraydatamode[idindex]['width']+"px และ สูง "+arraydatamode[idindex]['height']+"px เท่านั้น");
                                          return false;
                                          
                                          }
                                          
                                          
                        }
                        
                        
                        });
      }
    

}

function handleFilesImageSet(filesset,evt) {
    
    var f = filesset['target']['files'][0];

     if(checktypeexit(f.name)===false)
     {
     
     return false;
     }  
     
        // console.log(evt);
         var indexset= filesset['target']['id'].split("-");  
         dataBirdsendajax(f,indexset[1]);

}

function sendBirdfiledata(f,idindex,base64datastringnow){

        document.getElementById(arraydatamode[idindex]['target']).innerHTML ='<img class="loaderdata" alt="" src="'+baseinfo+'/img/loader.gif"><span id="numloaderdata-'+idindex+'" class="shownumloaderdata">0</span>';  
    
        var xmlhttp;
        
        if(window.XMLHttpRequest)
            xmlhttp = new XMLHttpRequest();
        else
            xmlhttp = new ActiveXObject("Microsoft.XMLDOM");

       
        xmlhttp.upload.onprogress = function(e) {
        
          if (e.lengthComputable) {

         var loaded = (e.loaded / e.total)*100;

        $("#numloaderdata-"+idindex).html(parseInt(loaded,10));
                           
         
         
         }
     
        }

        xmlhttp.upload.onload = function(e) {
            

        }

        xmlhttp.onreadystatechange = function() {
        
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {

                var myObject = eval('(' + xmlhttp.responseText + ')');
                var classsetdata="feedBnt";
                if(arraydatamode[idindex]['classdata'])
                {
                     classsetdata=arraydatamode[idindex]['classdata'];
                }
                $("#"+arraydatamode[idindex]['target']).html('<img id="'+arraydatamode[idindex]['imgid']+'" width="'+arraydatamode[idindex]['width']+'" height="'+arraydatamode[idindex]['height']+'"  class="'+classsetdata+'"   src="'+myObject.filelink+'">');
                $('#filebirdsavename-'+idindex).val(myObject.filename);
                var filebirdsettrigger='filebirdimage-'+idindex;
                $("#"+arraydatamode[idindex]['imgid']).click(function() {
                // console.log(imgid);
                $("#"+filebirdsettrigger).trigger('click');
                //console.log(idmode);
                
                });
            
            }
          
          
        }
        if(serviceinfo)
        {
        xmlhttp.open("post", serviceinfo+"uploadfile", true);
        }
        
        xmlhttp.setRequestHeader("If-Modified-Since", "Mon, 26 Jul 1997 05:00:00 GMT");
        xmlhttp.setRequestHeader("Cache-Control", "no-cache");
      //  xmlhttp.setRequestHeader("X-Requested-With", "XMLHttpRequest");
        //xmlhttp.setRequestHeader("X-File-Name", f.fileName);
      //  xmlhttp.setRequestHeader("X-File-Size", f.fileSize);
     //   xmlhttp.setRequestHeader("Content-Type", "multipart/form-data");
        var formData = new FormData();
       // console.log(f);
        formData.append("Filedata", f);
        formData.append("folder",arraydatamode[idindex]['foldername']);
//        formData.append("infofid", infofid);
        xmlhttp.send(formData);

}

function dataBirdsendajax(f,idindex){

    // if(f.size>500000)
//        {
//        resizeBirdimg(f,idindex);
//        return false;
//        }else
//        {
//        sendBirdfiledata(f,idindex);
//        
//        }  
        
        resizeBirdimg(f,idindex);  
   

}

function dropBird(evt) {
    evt.stopPropagation();
    evt.preventDefault();

    var files = evt.dataTransfer.files;
    var count = files.length;

    //console.log(evt);
    //console.log();

    if(checkBirdtypeexit(files[0].name)===false)
     {
     
     return false;
     }
 
    // dataBirdsendajax(files[0],indexset[1]);
    
    
    dataBirdsendajax(files[0],arraydatamode[evt.currentTarget.id]);
    // Only call the handler if 1 or more files was dropped.
    //if (count > 0)
      //  handleBirdFiles(files,evt);
}

function handleBirdFiles(filesset,evt) {
     // console.log(files);
      var f = filesset['target']['files'][0];

     if(checkBirdtypeexit(f.name)===false)
     {
     
     return false;
     }  
     
        // console.log(evt);
         var indexset= filesset['target']['id'].split("-");  
         dataBirdsendajax(f,indexset[1]);
}
function dragBirdEnter(evt) {
    evt.stopPropagation();
    evt.preventDefault();
}

function dragBirdExit(evt) {
    evt.stopPropagation();
    evt.preventDefault();
}

function dragBirdOver(evt) {
    evt.stopPropagation();
    evt.preventDefault();
}
function configBird(targetid,imgid,foldername,width,height,nameimage,checkvalidate,nocrop,classdata){
       if(!checkvalidate)
       {
       checkvalidate=false;
       }

        //$("#"+targetbirdname).attr("id",targetbirdname+"-"+targetid)
       var dropbox = document.getElementById(targetid);
       var nowid =idmode;
       dropbox.addEventListener("drop", dropBird, false);
       dropbox.addEventListener("dragenter", dragBirdEnter, false);
       dropbox.addEventListener("dragexit", dragBirdExit, false);
       dropbox.addEventListener("dragover", dragBirdOver, false);
       
       arraydatamode[idmode]={"target":targetid,"checkvalidate":checkvalidate,"id":idmode,"width":width,"height":height,"foldername":foldername,"imgid":imgid,"nameimage":nameimage,"nocrop":nocrop,"classdata":classdata};
       arraydatamode[targetid]=idmode;

       
       
       $("#"+targetid).before($('<input accept="image/*" value="1" name="filebirdimage" type="file" id="filebirdimage-'+idmode+'" style="display: none;"  ><input name="'+nameimage+'"  type="hidden" id="filebirdsavename-'+idmode+'"   >'));
       document.getElementById('filebirdimage-'+idmode).addEventListener('change', handleBirdFiles, false);
       var  filebirdsettrigger=  'filebirdimage-'+idmode;
       $("#"+imgid).click(function() {
                // console.log(imgid);
                $("#"+filebirdsettrigger).trigger('click');
                //console.log(idmode);
                
       });
       
     //  console.log(arraydatamode);
        idmode++; 
        return nowid;  

}
